#include "BeginScene.h"

Scene* Begin::createScene() {
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = Begin::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool Begin::init() {
    //////////////////////////////
    // 1. super init first
    if(!Layer::init()) {
        return false;
    }
    
    mainLayer = Layer::create();
    addChild(mainLayer);
 
    initStep(Step::STEP_LOGO);
    scheduleUpdate();
    return true;
}

void Begin::onExit() {
    Layer::onExit();
    unscheduleUpdate();
}

// set curStep based on step passing through
void Begin::initStep(Step step) {
    float duration = -1;
    switch (step) {
        case STEP_LOGO:
            duration = DURATION_STEP_LOGO;
            break;
        case STEP_WARNING:
            duration = DURATION_STEP_WARNING;
            break;
        case STEP_MAGIC:
            duration = DURATION_STEP_MAGIC;
            break;
        case STEP_FINAL:
            // do nothing in here
            break;
        default:
            assert(NULL);
            break;
    }
    
    curStep.step = step;
    curStep.duration = duration;
}

void Begin::doStep(Step step) {
    mainLayer->removeAllChildren();
    switch (step) {
        case STEP_LOGO: {
            break;
        }
        case STEP_WARNING: {
            break;
        }
        case STEP_MAGIC: {
            break;
        }
        case STEP_FINAL:
            // do nothing in here
            break;
        default:
            assert(NULL);
            break;
    }
}

void Begin::update(float dt) {
    updateStep(dt);
}

// determine what's next show
void Begin::updateStep(float dt) {
    if (curStep.step == STEP_FINAL) {
        return;
    }
    
    // get default duration, next step
    float defaultDuration = -1;
    Step nextStep;
    switch (curStep.step) {
        case STEP_LOGO: {
            defaultDuration = DURATION_STEP_LOGO;
            nextStep = STEP_WARNING;
            break;
        }
        case STEP_WARNING: {
            defaultDuration = DURATION_STEP_WARNING;
            nextStep = STEP_MAGIC;
            break;
        }
        case STEP_MAGIC: {
            defaultDuration = DURATION_STEP_MAGIC;
            nextStep = STEP_FINAL;
            break;
        }
        default:
            break;
    }
    
    // execute/do step
    if (curStep.duration == defaultDuration) {
        doStep(curStep.step);
    }
    
    // decrease duration
    curStep.duration -= dt;
    
    // check whether step end
    if (curStep.duration < 0) {
        initStep(nextStep);
    }
}